package com.lishe.lishe.lishe.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.lishe.lishe.lishe.base.BaseApplication;
import com.lishe.lishe.lishe.common.Globals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static android.content.Context.MODE_PRIVATE;

/**
 * Created by zhcase on 2017/9/5.
 */

public class SharedPreferencesUtils {

    /**
     * 保存激励模块个人中心信息
     */
    private static final String Excitation_person_img = "excitationPersonImg";
    private static final String Excitation_person_name = "excitationPersonName";


    /**
     * 保存激励模块用户头像和名字
     *
     * @param context
     * @param imgUrl
     * @param name
     */
    public static void saveExcitationPersonInfo(Context context, String imgUrl, String name) {
        if (imgUrl == null && name == null) {
            return;
        }
        SharedPreferences sharedPreferences = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(Excitation_person_img, imgUrl);
        if (!TextUtils.isEmpty(name)) {
            editor.putString(Excitation_person_name, name);
        }
        editor.commit();// 提交修改
    }

    public static String getExcitationPersonImg(Context context) {
        SharedPreferences sharedPreferences = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, MODE_PRIVATE);
        return sharedPreferences.getString(Excitation_person_img, null);
    }

    public static String getExcitationPersonName(Context context) {
        SharedPreferences sharedPreferences = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, MODE_PRIVATE);
        return sharedPreferences.getString(Excitation_person_name, null);
    }

    /**
     * 保存token
     */
    public static void saveToken(String token, Context context) {
        LogUtil.i("保存token", token);
        SharedPreferences tokenShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = tokenShare.edit();
        ed.putString("token", token);
        ed.commit();
    }

    /**
     * 获取token
     */
    public static String getToken(Context context) {
        SharedPreferences tokenShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        String token = tokenShare.getString("token", null);
        return token;
    }

    /**
     * 清除token
     */
    public static void removeToken(Context context) {
        SharedPreferences tokenShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = tokenShare.edit();
        ed.remove("token");
        ed.commit();
    }

    /**
     * 保存userid
     */
    public static void saveUserId(String userId, Context context) {
        SharedPreferences userIdShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = userIdShare.edit();
        ed.putString("userId", userId);
        ed.commit();
    }

    /**
     * 清除userid
     */
    public static String getUserId(Context context) {
        SharedPreferences userIdShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        return userIdShare.getString("userId", null);
    }

    /**
     * 清除userid
     */
    public static void removeUserId(Context context) {
        SharedPreferences userIdShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = userIdShare.edit();
        ed.remove("userId");
        ed.commit();
    }

    /**
     * 保存手机号
     */
    public static void savePhone(String phone, Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = phoneShare.edit();
        ed.putString("phone", phone);
        ed.commit();
    }

    /**
     * 保存手机号
     */
    public static String getPhone(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        return phoneShare.getString("phone", null);
    }

    /**
     * 清除手机号
     */
    public static void removePhone(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = phoneShare.edit();
        ed.remove("phone");
        ed.commit();
    }

    /**
     * 保存激励模块userId
     */
    public static void saveExcitationUserId(String userId, Context context) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("excitationUserId", userId);
        ed.commit();
    }

    /**
     * 获取激励模块UserId
     */
    public static String getExcitationUserId(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        return phoneShare.getString("excitationUserId", null);
    }

    /**
     * 保存企业数
     */
    public static void saveComSize(int comSize, Context context) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putInt("comSize", comSize);
        ed.commit();
    }

    /**
     * 获取激励模块UserId
     */
    public static int getComSize(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        return phoneShare.getInt("comSize", -1);
    }

    /**
     * 清除手机号
     */
    public static void removeComSize(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = phoneShare.edit();
        ed.remove("comSize");
        ed.commit();
    }

    /**
     * 保存极光推送RegistrationID
     */
    public static void saveRegistrationID(String RegistrationID) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("RegistrationID", RegistrationID);
        ed.commit();
    }

    /**
     * 获取极光推送RegistrationID
     */
    public static String getRegistrationID() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        return phoneShare.getString("RegistrationID", null);
    }

    /**
     * 保存从服务器获取的新的版本信息
     */
    public static void saveAppVersionAndCode(String app_version, int app_code) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("app_version", app_version);
        ed.putInt("app_code", app_code);
        ed.commit();
    }

    public static int getAppCode() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        return phoneShare.getInt("app_code", 0);
    }

    public static String getAppVersion() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        return phoneShare.getString("app_version", null);
    }

    /**
     * 保存通告id
     */
    public static void saveNoticeID(String noticeId) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("notice_id", getNoticeID() + noticeId + ",");
        ed.commit();
    }

    public static String getNoticeID() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        return phoneShare.getString("notice_id", "");
    }

    /**
     * 保存首页数据
     */
    public static void saveHomeData(String data) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("HomeData", data);
        ed.commit();
    }

    /**
     * 取出首页数据
     */
    public static String getHomeData() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        return phoneShare.getString("HomeData", "");
    }

    /**
     * 清除通告记录
     */
    public static void clearNoticeID() {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("notice_id", "");
        ed.commit();
    }


    public static final String SearchHitstory = "SearchHitstory";
    /**
     * 保存搜索历史
     * @param history
     */
    public static void saveSearchHitstory(String history) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        List<String> list = getSearchHitstory();
        for (int i = 0; i < list.size(); i++) {
            if (history.equals(list.get(i))) {
                list.remove(i);
            }
        }
        list.add(0, history);
        //写入
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i < 10) {
                sb.append(list.get(i) + ",");
            } else break;
        }
        ed.putString(SearchHitstory, sb.substring(0, sb.length() - 1));
        ed.commit();
    }

    /**
     * 取出搜索历史
     */
    public static List<String> getSearchHitstory() {
        SharedPreferences sf = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        String[] historys = sf.getString(SearchHitstory, "").split(",");
        return new ArrayList<>(Arrays.asList(historys));
    }

    /**
     * 保存搜索历史
     */
    public static void clearSearchHitstory() {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString(SearchHitstory, "");
        ed.commit();
    }

    /**
     * 清除所有sp用户信息
     */
    public static void clearUserAll() {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_USER, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.clear();
        ed.commit();
    }

    /**
     * 清除所有sp应用信息
     */
    public static void clearBaseAll() {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_BASE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.clear();
        ed.commit();
    }
}

